package edu.mse.camel.client.session.views;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.events.server.QuerySessionListEvent;
import edu.mse.camel.client.events.server.QuerySessionListEvent.SessionList;
import edu.mse.camel.client.session.team.TeamVewConstants;

public class PlaybackDialog extends DefaultDialog {

	String[] sessionName;
	private List list;
	private Text speedField;
	private Label speedLabel;
	private float speed;

	// Create a hashmap containing the session ID and the session title
	Map<Long, String> sessionIDMap = new HashMap<Long, String>();

	public PlaybackDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == 0){
			sessionName = list.getSelection();
			speed = Float.parseFloat(speedField.getText());
		}
		else{
			sessionName = null;
		}

		super.buttonPressed(buttonId);
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {

		Composite comp = (Composite) super.createDialogArea(parent);
		comp.getShell().setText(DialogMessages.SessionDialog_Playback);
		
		GridLayout layout = (GridLayout) comp.getLayout();
		layout.numColumns = 2;

		// Subscribe to the event
		QuerySessionListEvent qslEvent = new QuerySessionListEvent();
		EventManager.getInstance().dispatchEventSynchronous(qslEvent, true);
		final java.util.List<SessionList> sessionList = qslEvent.getSessionList();

		Label selectionQuesLabel = new Label(comp, SWT.LEFT);
		selectionQuesLabel.setText("Which session you want to join? ");
		GridData data = new GridData();
		data.horizontalSpan = 2;
		selectionQuesLabel.setLayoutData(data);

		list = new List(comp, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
		querySessionName(list, sessionList);

		final Text description = new Text(comp, SWT.WRAP | SWT.MULTI);
		description.setEditable(false);
		data = new GridData(SWT.WRAP);
		data.verticalAlignment = SWT.TOP;
		data.horizontalAlignment = SWT.LEFT;
		data.widthHint = getInitialSize().x / layout.numColumns ;
		data.heightHint = getInitialSize().y * 4/5 ;
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		description.setLayoutData(data);
		list.setLayoutData(data);
		
		list.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent event) {
				String[] selection = list.getSelection();
				description.setText(
						getDiscription(selection[0], sessionList));
				description.redraw();
			}

			public void widgetDefaultSelected(SelectionEvent event) {
			}
		});
		
		speedLabel = new Label(comp, SWT.LEFT);
		speedLabel.setText("Please enter the speed for playback ");
		data = new GridData();
		speedLabel.setLayoutData(data);
		
		speedField = new Text(comp, SWT.SINGLE| SWT.BORDER_SOLID);
		data = new GridData(GridData.FILL_HORIZONTAL);
		speedField.setLayoutData(data);

		//need to add listener to validate the input value
		
		return comp;
	}

	public String getSessionName() {
		if (sessionName == null)
			return "";
		else
			return sessionName[0];
	}
	
	public float getSpeed() {
		return speed;
	}

	public void querySessionName(org.eclipse.swt.widgets.List list,
			java.util.List<SessionList> sessionList) {

		for (int i = 0; i < sessionList.size(); i++) {
			SessionList aSession = (SessionList) sessionList.get(i);
			String sessionInfo = aSession.getsessionId().toString()
					+ TeamVewConstants.SESSION_ID_TITLE_SEPARATOR
					+ aSession.getTitle();
			list.add(sessionInfo);

		}
	}

	public String getSessionDescription(String str, 
							java.util.List<SessionList> sessionList) {
		Map<String, String> sessionInfo = new HashMap<String, String>();

		for (SessionList aSession : sessionList) {
			String sessionIdentifier = aSession.getsessionId().toString()
					+ TeamVewConstants.SESSION_ID_TITLE_SEPARATOR
					+ aSession.getTitle();
			sessionInfo.put(sessionIdentifier, aSession.getDescription());
		}

		if (sessionInfo.containsKey(str))
			return sessionInfo.get(str).toString();
		else
			return "";
	}
	
	public String getDiscription(String index, java.util.List<SessionList> sessionList) {
		String str = "";
		str = getSessionDescription(index, sessionList);
		return str;
	}	
	
}
